package com.bilibili.juc.AtomicTest;

import java.util.concurrent.CountDownLatch;

/**
 * @author 小泽
 * @create 2022-09-23  9:42
 * 记得每天敲代码哦
 */
//方法二-减法计数器CountDownLatch
public class AtomicIntegerDemo2 {
    public static final int SIZE = 50;
    public static void main(String[] args) throws InterruptedException {
        MyNumber myNumber = new MyNumber();
        CountDownLatch countDownLatch = new CountDownLatch(SIZE);
        for(int i = 1;i <= SIZE;i ++){
            new Thread(() -> {
                try {
                    for(int j = 1;j <= 1000;j ++){
                        myNumber.addPlusPlus();
                    }
                } finally {
                    countDownLatch.countDown();
                }
            },String.valueOf(i)).start();
        }
        countDownLatch.await();
        System.out.println(Thread.currentThread().getName()+"\t"+"result: "+myNumber.atomicInteger);
    }
}
//main  result: 50000
